package com.spring.aop;

/**
 * Aop 上下文，保存代理对象引用
 *
 * @author ljj
 */
public class AopContext {
	private static final ThreadLocal<Object> localProxy = new ThreadLocal<>();
	
	private AopContext() {
	}
	
	public static Object setCurrentProxy(Object proxy) {
		Object oldProxy = localProxy.get();
		if (proxy == null) {
			localProxy.remove();
		} else {
			localProxy.set(proxy);
		}
		return oldProxy;
	}
	
	public static Object currentProxy() {
		Object proxy = localProxy.get();
		if (proxy == null) {
			throw new RuntimeException("当前对象并未被代理");
		}
		return proxy;
	}
	
	public static void clear(){
		localProxy.remove();
	}
}
